package com.wenwan.manage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwan.manage.mapper.ReportMapper;
import com.wenwan.manage.pojo.LimitPage;
import com.wenwan.manage.pojo.Report;
import com.wenwan.manage.service.ArticleFeedbackExamineService;
import com.wenwan.manage.service.ArticleFeedbackService;
import com.wenwan.manage.service.GiveGetFeedbackExamineService;
import com.wenwan.manage.service.GiveGetFeedbackService;
import com.wenwan.manage.service.ReportService;
import com.wenwan.manage.setting.RegExpSetting;
import com.wenwan.manage.setting.StatusSetting;

@Service
public class ReportServiceImpl implements ReportService {

	@Autowired
	private ReportMapper reportMapper;

	@Override
	public Map<String, Object> selectAll(Integer currentPage, Integer pageSize, Boolean isForHandled,
			Boolean isForHandleing) {
		// TODO Auto-generated method stub
		// DataToObject<List<Report>> dto = new DataToObject<List<Report>>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (currentPage != null && currentPage < 1) {
			dto.put("data", false);
			dto.put("msg", "currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (pageSize != null && pageSize < 1) {
			dto.put("data", false);
			dto.put("msg", "pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("currentPage", (currentPage - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("isForHandled", isForHandled);
		map.put("isForHandleing", isForHandleing);

		List<Report> data = reportMapper.selectAll(map);
		LimitPage limitPage = new LimitPage();
		limitPage.setCurrentPage(currentPage);
		limitPage.setPageSize(pageSize);
		Integer maxSize = reportMapper.selectMaxSize(map);
		limitPage.setMaxSize(maxSize);

		// dto.setLimitPage(limitPage);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("limitPage", limitPage);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectOneById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<Report> dto = new DataToObject<Report>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);

		Report data = reportMapper.selectOneById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Autowired
	private ArticleFeedbackExamineService articleFeedbackExamineService;

	@Autowired
	private GiveGetFeedbackExamineService giveGetFeedbackExamineService;

	@Override
	public Map<String, Object> examine(String examineId, String belong, Boolean isAdopt) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (examineId != null && !examineId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "examineId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("examineId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (belong != null && belong.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "belong" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("belong" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if ("普通文章".equals(belong)) {
			return articleFeedbackExamineService.create(examineId, isAdopt);
		} else if ("供求文章".equals(belong)) {
			return giveGetFeedbackExamineService.create(examineId, isAdopt);
		}
		// dto.setData(false);
		dto.put("data", false);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Autowired
	private ArticleFeedbackService articleFeedbackService;

	@Autowired
	private GiveGetFeedbackService giveGetFeedbackService;

	@Override
	public Map<String, Object> deleteOneById(String id, String belong) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (belong != null && belong.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "belong" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("belong" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if ("普通文章".equals(belong)) {
			return articleFeedbackService.deleteById(id);
		} else if ("供求文章".equals(belong)) {
			return giveGetFeedbackService.deleteById(id);
		}
		// dto.setData(false);
		dto.put("data", false);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

}
